// Package config Package logicConfig -----------------------------
// @file      : cardGroupConfig.go
// @author    : WuZhouHai
// @contact   : wzh200x@126.com
// @time      : 2024/3/5 16:23
// @desc      :
// -------------------------------------------
package config

import (
	"context"
	"github.com/gogf/gf/v2/encoding/gjson"
	"phoenix/apps/examples/phoenix_app_dwys/model/gameConfig"
	"phoenix/phoenix/base"
	"phoenix/phoenix/common/logger"
	"phoenix/phoenix/interfaces"
)

type CardGroupConfig struct {
	base.BaseLogicConfig
}

func NewCardGroupConfig(configName string, configFile string, ctx context.Context) (interfaces.ILogicConfig, error) {
	return &CardGroupConfig{
		BaseLogicConfig: base.NewBaseLogicConfig(configName, configFile, ctx),
	}, nil
}

func (this *CardGroupConfig) InitLogicConfig() {
	//TODO implement me
	fileData, err := this.LoadConfigFile()
	if err != nil {
		logger.PhoenixLoggerIns().Errorf("CardGroupConfig load config %s error: %s", this.GetFileName(), err)
		return
	}
	if j, err := gjson.DecodeToJson(fileData); err != nil {
		logger.PhoenixLoggerIns().Errorf("CardGroupConfig decode config %s error: %s", this.GetFileName(), err)
	} else {
		var configs []gameConfig.CardGroup
		if err := j.Scan(&configs); err != nil {
			logger.PhoenixLoggerIns().Errorf("CardGroupConfig scan config %s error: %s", this.GetFileName(), err)
		}
		var extendData *gameConfig.CardGroupAnalysis
		for i, config := range configs {
			this.SetConfigItem(config.ID, &configs[i])
			if !this.CheckConfigExtendData(config.GroupId) {
				extendData = new(gameConfig.CardGroupAnalysis)
				extendData.GroupId = config.GroupId
				this.SetConfigExtendData(config.GroupId, extendData)
			} else {
				if datas, ok := this.GetConfigExtendData(config.GroupId).(*gameConfig.CardGroupAnalysis); ok {
					extendData = datas
				}
			}
			if extendData == nil {
				continue
			}
			sArray := [3]int32{config.ID, extendData.TotalWeight, extendData.TotalWeight + config.Weight}
			extendData.TotalWeight += config.Weight
			extendData.SearchArray.Append(sArray)
		}
	}
}
